package jpa.service.synchro.staging;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.Message;
import jpa.entitys.synchro.staging.MessagesGroup;
import jpa.entitys.synchro.staging.MmConvFactInb;
import jpa.service.synchro.common.JPAService;

public class MessageServices extends JPAService {	
	
	public MessageServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}

	private static final Logger log = Logger.getLogger(MessageServices.class);
	
	public Message findById(String id) throws Exception {		
					
		return super.ConvertToYoureObject(super.find(id, "Message.findById"));				
	}
	
	public List<Message> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "Message.findByInstanceNo"));				
	}
	
	public List<CommonEntity> findByCode(String code, MessagesGroup messagesGroup) throws Exception {				
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("code", code);
		mapWhereValue.put("messagesGroup", messagesGroup);
		
		return super.ConvertToYoureObject(super.find("Message.findByCode", mapWhereValue) );
	}
	
	public Message Saving(Message message) throws Exception {
		try{
			if(CommonService.IsNull(message.getMessagesId()))
			{
				String id = super.SequenceNext("MESSAGES");
				log.info("id : " + id);
				
				message.setMessagesId(id);
								
				String stringdateTime = new SimpleDateFormat("yyyy-MM-dd//HH-mm-ss").format(Calendar.getInstance().getTime());
				
				message.setCreatedBy("sys");
				message.setCreatedOn(stringdateTime);
				
				message.setUpdatedBy("sys");
				message.setUpdatedOn(stringdateTime);
				
				message.setSyncCreatedby("sys");
				message.setSyncCreatedon(Calendar.getInstance().getTime());
				
				message.setSyncUpdatedby("sys");
				message.setSyncUpdatedon(Calendar.getInstance().getTime());
				
				message.setIsactive("Y");
				message.setIssync("N");
								
				message = (Message)super.inserted(message);
				
			}else
				message = (Message)super.updated(message);			
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return message;
	}
	
	
	public List<Message> Inserted(List<Message> message_list) throws Exception {
		try{
			int row = 0;
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			
			String stringdateTime = new SimpleDateFormat("yyyy-MM-dd//HH-mm-ss").format(Calendar.getInstance().getTime());
			for(Message message : message_list)
			{
				row++;
				if(CommonService.IsNull(message.getMessagesId()))
				{
					String id = super.SequenceNext("MESSAGES");
					log.info("id : " + id);
					
					message.setMessagesId(id);												
					
					message.setCreatedBy("sys");
					message.setCreatedOn(stringdateTime);
					
					message.setUpdatedBy("sys");
					message.setUpdatedOn(stringdateTime);
					
					message.setSyncCreatedby("sys");
					message.setSyncCreatedon(Calendar.getInstance().getTime());
					
					message.setSyncUpdatedby("sys");
					message.setSyncUpdatedon(Calendar.getInstance().getTime());
					
					message.setIsactive("Y");
					message.setIssync("N");
					common_lst.add(message);
					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return message_list;
	}
	
}
